package com.star.spring.deferred.service.completable;


import com.star.spring.deferred.utils.CommonUtils;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

/**
 * @description: 有返回值的异步任务
 * @author: star
 * @create: 2024-12-22 17:24
 **/

public class SupplyAsyncDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CommonUtils.log("main start...");
        CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> {
            return CommonUtils.readFileToText("news.txt");
        });


        CompletableFuture<String[]> supplyAsyncFilter = CompletableFuture.supplyAsync(() -> {

            return CommonUtils.readFileToTextArray("filter_words.txt");
        });


        // 获取子线程中的结果
        String supplyAsyncResult = supplyAsync.get();
        CommonUtils.log(supplyAsyncResult);

        String[] words = supplyAsyncFilter.get();
        CommonUtils.log(Arrays.toString(words));

        CommonUtils.log("main end...");
    }
}
